<script setup>
import Node from "./base/Node.vue";
import { computed, ref } from "vue";
import nodeMixin from "../NodeMixin";

const props = defineProps({
  ...nodeMixin.props
});
const emit = defineEmits(["insertNode", "delete", "select"]);
const _value = defineModel();
const showErr = ref(false);
const errInfo = ref(null);

const content = computed(() => {
  let desc = "请设置节点及任务属性";
  return desc;
});

/**
 * 节点规则校验函数
 * @param errs 错误信息
 */
function validate(errs) {
  const prop = _value.value.props;
  if (prop.tasks.length > 0) {
    prop.tasks.forEach(item => {
      switch (item.jobType) {
        case "0":
          if (!item.userId && (!item.userList || item.userList.length < 1)) {
            errInfo.value = `${item.name}未指定具体审批人`;
            errs.push(`${_value.value.name}的${item.name}未指定审批人`);
            return;
          }
          break;
        case "1":
          if (!item.roleIds && (!item.roleList || item.roleList.length < 1)) {
            errInfo.value = `${item.name}未设置审批角色`;
            errs.push(`${_value.value.name}的${item.name}未设置角色`);
            return;
          }
          break;
        case "3":
          break;
        case "4":
          if (item.dynamicUserType === "1") {
            if (!item.dynamicUserId) {
              errInfo.value = `${item.name}未设置指定用户主管`;
              errs.push(`${_value.value.name}的${item.name}未设置指定用户主管`);
              return;
            }
          }
          break;
        case "5":
          if (item.dynamicUserType === "1") {
            if (!item.dynamicUserId) {
              errInfo.value = `${item.name}未设置指定用户主管`;
              errs.push(`${_value.value.name}的${item.name}未设置指定用户主管`);
              return;
            }
          }
          break;
        case "6":
          if (!item.dynamicDeptId) {
            errInfo.value = `${item.name}未设置指定部门主管`;
            errs.push(`${_value.value.name}的${item.name}未设置指定部门主管`);
            return;
          }
          break;
        case "7":
          if (!item.dynamicUserKey) {
            errInfo.value = `${item.name}未设置动态办理人表单字段`;
            errs.push(
              `${_value.value.name}的${item.name}未设置动态办理人表单字段`
            );
            return;
          }
          break;
        case "8":
          if (!item.dutyId) {
            errInfo.value = `${item.name}未设置指定岗位`;
            errs.push(`${_value.value.name}的${item.name}未设置指定岗位`);
            return;
          }
          break;
        case "9":
          if (!item.dutyId) {
            errInfo.value = `${item.name}未设置指定岗位`;
            errs.push(`${_value.value.name}的${item.name}未设置指定岗位`);
            return;
          }
          if (!item.dynamicDeptId) {
            errInfo.value = `${item.name}未设置指定部门`;
            errs.push(`${_value.value.name}的${item.name}未设置指定部门`);
            return;
          }
          break;
        case "10":
          if (!item.dynamicFunctionId) {
            errInfo.value = `${item.name}未选择动态函数`;
            errs.push(`${_value.value.name}的${item.name}未选择动态函数`);
            return;
          }
          break;
      }
    });
  }
  if (errs.length > 0) {
    showErr.value = true;
  } else {
    showErr.value = false;
  }
  return;
}

const handleNodeSelect = taskIndex => {
  if (!taskIndex) {
    taskIndex = 0;
  }
  emit("select", { node: _value.value, taskIndex: taskIndex });
};

defineExpose({ validate });
</script>

<template>
  <node
    v-model="_value"
    :readonly="props.readonly"
    :show-error="showErr"
    :error-info="errInfo"
    :index="index"
    type="Job"
    :header-color="props.headerColor"
    :flowHistory="flowHistory"
    header-icon="stamp"
    :content="content"
    @select="handleNodeSelect"
    @insertNode="type => emit('insertNode', props.branch, props.index, type)"
    @delete="emit('delete', props.branch, props.index)"
  />
</template>

<style scoped></style>
